package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.dao.*;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderSubmitMapper orderSubmitMapper;

    /**
     * 分页查询预约信息
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // Page<OrderFindPageResult> page = orderMapper.findPage(queryPageBean.getQueryString());
        Page<Order> page = orderMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *
     * @param orderUpdateRequestBody
     */
    public void update(OrderUpdateRequestBody orderUpdateRequestBody) {
        orderMapper.update(orderUpdateRequestBody);
    }

    /**
     * 根据预约id查询预约信息
     * @param id
     * @return
     */
    public OrderInformation findById(Integer id) {

        OrderInformation orderInformation = new OrderInformation();
        Order order = orderMapper.findById(id);
        orderInformation.setOrderType(order.getOrderType());
        orderInformation.setOrderDate(order.getOrderDate());
        orderInformation.setSetmeal(setmealMapper.getSetmealNameById(order.getSetmealId()));
        orderInformation.setMember(memberMapper.findById(order.getMemberId()));
        return null;
    }

    /**
     * 提交预约
     * @param submitOrder
     */
    public Result submitOrder(SubmitOrder submitOrder) {

        Result result = new Result();
        //根据日期查询可预约人数
        Integer number = orderSettingMapper.getNumberByOrderDate(submitOrder.getOrderDate());
        if (number == null){
            result.setFlag(false);
            result.setMessage(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //根据日期查询已预约人数
        Integer reservations =orderSettingMapper.getOrderReservationsByOrderDate(submitOrder.getOrderDate());
        if (reservations >= number){
            result.setFlag(false);
            result.setMessage(MessageConstant.ORDER_FULL);
        }
        //预约人数加一
        orderSettingMapper.editReservationsByOrderDate(submitOrder.getOrderDate());
        //添加预约信息
        orderMapper.add(submitOrder);
        //如果一个用户已经预约过当天的套餐就不能再预约
        if (orderSubmitMapper.getByIdCard(submitOrder.getIdCard()) != null ){
            result.setFlag(false);
            result.setMessage(MessageConstant.HAS_ORDERED);
            return result;
        }
        // 如果当前用户不是会员,需要自动完成注册
        if (memberMapper.findByTelephone(submitOrder.getTelephone()) == null){
            Member member = new Member();
            member.setName(submitOrder.getName());
            member.setPhoneNumber(String.valueOf(submitOrder.getTelephone()));
            member.setIdCard(submitOrder.getIdCard());
            member.setSex(submitOrder.getSex());
            member.setRegTime(submitOrder.getOrderDate());
            memberMapper.add(member);
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.ORDER_SUCCESS);
        return result;
    }
}
